#!/bin/sh

IP=""
gateway=""
netmask=""
linuxType=""
append=0
proto=""
device=""
while getopts  d:p:w:t:i:g:m:a name
do
	case $name in
		t) linuxType=$OPTARG
		   echo $OPTARG
		;;
		i) IP=$OPTARG
		;;
		g) gateway=$OPTARG
		;;
		m) netmask=$OPTARG
		;;
		a) append=1
		;;
		p) proto=$OPTARG
		;;
		d) device=$OPTARG
		;;
		w) path=$OPTARG
		;;
		\?) echo "Usage"
		;;
	esac
done
echo "I FUCK!"
echo $linuxType
if [ "$proto" = "static" ] && ( [ -z "$linuxType" ] || [ -z "$IP" ] \
			|| [ -z "$netmask" ] || [ -z "$device" ] )
then
	echo "Usage: modifyNetwork.sh -p static -i IP -m netmask -d deviceName -t linuxType [ -g gateway -a]"
	exit -2;
elif [ "$proto" != "static" ] && ( [ -z "$device" ] || [ -z "$linuxType" ] )
then
	echo "Usage:"
	exit -2;
fi

if [ "$linuxType" = "centos" ] || [ "$linuxType" = "redhat" ] || [ "$linuxType" = "fedora" ]
then
	if [ $proto == "static" ]
	then
	cat > "$path/etc/sysconfig/network-scripts/ifcfg-$device" <<!NETWORK!
DEVICE=$device
BOOTPROTO=static
IPADDR=$IP
NETMASK=$netmask
ONBOOT=yes
TYPE=Ethernet
!NETWORK!
	else
	cat > "$path/etc/sysconfig/network-scripts/ifcfg-$device" <<!NETWORK!
DEVICE=$device
BOOTPROTO=dhcp
ONBOOT=yes
TYPE=Ethernet
!NETWORK!
	fi
	if [ -n "$gateway" ]
	then
	rm -f $path'/etc/sysconfig/network.bk'
	eval $(awk 'BEGIN { found=0;OFS="=";FS="=";}\
		 {  if ($1 == "NETWORK" )\
			{\
				$2="yes";\
			}\
			else if ($1 == "GATEWAY")\
			{\
				$2="'$gateway'";\
				found=1;\
			}\
			print $0 > "'$path'/etc/sysconfig/network.bk"\
		 } END { printf("found=%d",found);}' $path'/etc/sysconfig/network')
	if [ $found -eq 0 ]
	then
		echo "GATEWAY=$gateway" >> $path'/etc/sysconfig/network.bk'
	fi
	mv $path'/etc/sysconfig/network.bk' $path'/etc/sysconfig/network'
	rm -f $path'/etc/sysconfig/network.bk'

	else
	rm -f $path'/etc/sysconfig/network.bk'
       	awk 'BEGIN { found=0;OFS="=";FS="=";}\
                 {  if ($1 == "NETWORK" )\
                        {\
                                $2="yes";\
                        }\
                        else if ($1 == "GATEWAY")\
                        {\
				
                                $0="";\
                        }\
                        print $0 > "'$path'/etc/sysconfig/network.bk"\
                 } END { printf("found=%d",found);}' $path'/etc/sysconfig/network'
        mv $path'/etc/sysconfig/network.bk' $path'/etc/sysconfig/network'
	rm -f $path'/etc/sysconfig/network.bk'

	fi

elif [ "$linuxType" = "debian" ] || [ "$linuxType" = "ubuntu" ]
then
	if [ $append -eq 1 ]
	then
		echo "auto  $device" >>$path'/etc/network/interfaces'
		echo "iface $device inet $proto" >> $path'/etc/network/interfaces' 
	else
		echo "auto lo" >$path'/etc/network/interfaces' 
		echo "iface lo inet loopback" >> $path'/etc/network/interfaces'
		echo "auto $device">>$path'/etc/network/interfaces'
		echo "iface $device inet $proto" >> $path'/etc/network/interfaces'
	fi
	if [ $proto == "static" ]
	then
		echo "  address $IP" >> $path'/etc/network/interfaces'
		echo "  netmask $netmask" >> $path'/etc/network/interfaces'
		if [ -n "$gateway" ] 
		then
			echo "  gateway $gateway" >> $path'/etc/network/interfaces'
		fi
	fi
else
	echo " don't support this linux system"
	exit -1;
fi
